package com.flute.icrawler.app.processor.fetch;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.flute.icrawler.app.entity.CrawlResult;
import com.flute.icrawler.app.processor.AbstractProcessor;
import com.flute.icrawler.app.util.ParameterKey;
import com.flute.icrawler.app.util.TextUtil;
import com.flute.icrawler.framework.autoadjust.FetchAdjuster.FetchStatus;
import com.flute.icrawler.framework.framework.CrawlUrl;

public class FetchFile extends AbstractProcessor {

	private static final Logger LOGGER = LoggerFactory
			.getLogger(FetchFile.class);

	@Override
	public void process(CrawlUrl url) {
		// TODO Auto-generated method stub
		File file = new File(url.getUrl());

		if (!file.isFile()) {
			LOGGER.debug("not a file {}", url.getUrl());
			return;
		}
		CrawlResult result = (CrawlResult) url.getResultParameter(
				ParameterKey.RESULT_CRAWL).getValue();

		try {
			BufferedInputStream bufferedInputStream = new BufferedInputStream(
					new FileInputStream(file));

			ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
			byte[] bs = new byte[4096];
			int len = -1;
			while (-1 != (len = bufferedInputStream.read(bs))) {
				byteArrayOutputStream.write(bs, 0, len);
			}

			byte[] contentBytes = byteArrayOutputStream.toByteArray();
			result.setUrl(url.getUrl());
			result.setContent(contentBytes);
			result.setCharSet(TextUtil.findCharset(new String(contentBytes)));
			result.setFetchStatus(FetchStatus.Success);
			result.setContentType(file.getName().substring(
					file.getName().lastIndexOf(".") + 1));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			LOGGER.error("{}", e);
			result.setFetchStatus(FetchStatus.Invalid);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			LOGGER.error("{}", e);
			result.setFetchStatus(FetchStatus.Invalid);
		}

		LOGGER.info("fetch ok {}", url.getUrl());
	}

}
